package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.MediationReceive;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Strings;

public class ApprovalReceiveController extends Controller {


  public void add() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int mid = Strings.getInt(req, "mid", Consts.DEFAULT_BAD_ID);
    final String openid = Strings.getString(req, "openid", "");
    final String type = Strings.getString(req, "type", "");
    if (StrKit.notBlank(openid)) {
      result = MediationReceive.add(mid, type, openid);
    }
    renderJson(result.toJson());
  }

  public void list() {
    JSONObject req = Strings.toJSONObject(this);
    final int page = Strings.getInt(req, "page", Consts.DEFAULT_PAGE);
    final int size = Strings.getInt(req, "size", Consts.DEFAULT_PAGE_SIZE);
    final JSONArray filter = Strings.getJSONArray(req, "filter", null);
    renderJson(Strings
        .formatJsonDateTime(
            Strings.formatPage(MediationReceive.list(page, size, filter))));
  }

}
